package JUC.UnSafeDemo;


import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * @author CKC
 * @DateTime 2021/6/8 15:28
 * <p>
 * Unsafe 对象提供了非常底层的，操作内存、线程的方法，Unsafe 对象不能直接调用，只能通过反射获得
 */
public class UnsafeAccessor {
    private static final Unsafe UNSAFE;

    static {
        try {
            Field thUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            thUnsafe.setAccessible(true);
            UNSAFE = (Unsafe) thUnsafe.get(null);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new Error(e);
        }
    }

    public static Unsafe getUnsafe() {
        return UNSAFE;
    }
}
